package module.scene
{
	import com.darwin.darwinIsometric.core.IsoObject;
	import com.darwin.darwinIsometric.core.Point3D;
	import com.darwin.darwinIsometric.utils.IsoUtils;
	
	import flash.display.BlendMode;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import main.App;
	import main.mvc.GController;
	
	import module.scene.consts.SceneConst;
	
	public class FogManager
	{
		
//		private var _areaList:Vector.<Shape>;
		private var _areaDic:Dictionary;
		private var _fogLayer:Sprite;
		private var _areaContainer:Sprite;
		public static var instance:FogManager;

		public function FogManager(fogLayer:Sprite)
		{
			if(instance == null)
			{
				_fogLayer = fogLayer;
				_fogLayer.blendMode = BlendMode.LAYER;
				_fogLayer.graphics.beginFill(0x111111,0.5);
				_fogLayer.graphics.drawRect(0,0,App.appStage.stageWidth,App.appStage.stageHeight);
				_fogLayer.graphics.endFill();
				init();
				instance = this;
			}else{
				throw new Error("实例化单例出错 -- FogManager");
			}

		}
		
		
		private function init():void
		{
			_areaContainer = new Sprite;
			_areaContainer.blendMode = BlendMode.ERASE;
			_fogLayer.addChild(_areaContainer);
			_areaDic = new Dictionary();
			App.appStage.addEventListener(Event.RESIZE,onResize);
		}
		
		protected function onResize(event:Event):void
		{
			_fogLayer.graphics.clear();
			_fogLayer.graphics.beginFill(0x111111,0.5);
			_fogLayer.graphics.drawRect(0,0,App.appStage.stageWidth,App.appStage.stageHeight);
			_fogLayer.graphics.endFill();
		}
		
		/**
		 *	更新迷雾层坐标偏移 
		 * @param contextX 迷雾层对应的isoWorld的contextX
		 * @param contextY 迷雾层对应的isoWorld的contextY
		 * 
		 */
		public function updateContextXY(contextX:Number,contextY:Number):void
		{
			_areaContainer.x = contextX;
			_areaContainer.y = contextY;
		}
		
		/**
		 * 更新可视区域位置 
		 * @param target 要打开可视区域的目标(通常为isoObject)
		 * @param range 可视区域范围(如果大于0，则会根据所传值更新区域大小)
		 * @param show 是显示还是隐藏
		 */
		public function updateArea(target:IsoObject,range:int = 0,show:Boolean = true):void
		{
			var $area:Shape = _areaDic[target];
			
			if(show && range>0)
			{
				if($area == null)
				{
					$area =  new Shape();
//					$area.blendMode = BlendMode.ERASE;
					$area.filters = [new GlowFilter(0xff0000,1,range,range,5)];
					_areaContainer.addChild($area);
					_areaDic[target] =$area;
				}
				$area.x = target.x;
				$area.y = target.y;
				$area.alpha = 1;
				if(range>0 && range != int($area.name))
				{
					drawArea($area.graphics,target.floorWidth,target.floorHeight,range);
					$area.name = range.toString();
				}
			}else{
				if($area && $area.parent)
				{
					_areaContainer.removeChild($area);
				}
				delete _areaDic[target];
				$area = null;
			}

		}
		
		/**
		 *	判断传入物体是否处于迷雾中
		 * @param target
		 * @return 
		 * 
		 */
		public function isInFog(target:IsoObject):Boolean
		{
			if(target && target.parentWorld)
			{
				var point:Point= new Point(target.x + target.parentWorld.contentX,target.y + target.parentWorld.contentY);
				var objectList:Array = _fogLayer.getObjectsUnderPoint(point);
				while(objectList.indexOf(_fogLayer)!=-1)//排除fogLayer
				{
					objectList.splice(objectList.indexOf(_fogLayer),1);
				}
				return objectList.length <= 0;
			}
			return true;
		}
		
		private function drawArea(graphics:Graphics,floorWidth:Number,floorHeight:Number,range:Number,color:uint = 0xff0000,alpha:Number = 1):void
		{
			var pt:Point;
			graphics.clear();
			graphics.beginFill(color,alpha);
			pt = IsoUtils.isoToScreen(new Point3D(range/2 + floorWidth/2,0,range/2 + floorHeight/2));
			graphics.moveTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(-range/2 + floorWidth/2,0,range/2 + floorHeight/2));
			graphics.lineTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(-range/2 + floorWidth/2,0,-range/2 + floorHeight/2));
			graphics.lineTo(pt.x,pt.y);
			
			pt = IsoUtils.isoToScreen(new Point3D(range/2 + floorWidth/2,0,-range/2 + floorHeight/2));
			graphics.lineTo(pt.x,pt.y);
			graphics.endFill();
		}
	}
}